﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Transactions;
using COM;
namespace BLL
{
    public class NewMovePhotoBLL
    {
        /// <summary>
        /// 通过产品Id修改产品的照片移动状态
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <returns>true 修改成功，false 修改失败</returns>
        public bool UpdateOrderInfoMoveStatus(int orderInfoId)
        {
            bool result = true;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    //using (System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                    //{
                        
                        orderInfo oi = db.orderInfo.FirstOrDefault(o => o.OrderProductId == orderInfoId);
                        if (oi != null)
                        {
                            if (oi.status == (int)OrderInfoStatus.WaitPayAndWaitMovePhoto)//如果是等待移动和等待付款状态
                            {
                                oi.status = (int)OrderInfoStatus.WaitPay;//改成等待付款状态
                            }
                            else
                            {
                                oi.status = (int)OrderInfoStatus.Doing;//改成正常生产状态
                            }

                            //修改照片状态
                            if (!oi.photoInfo.IsLoaded)
                                oi.photoInfo.Load();
                            foreach (var item in oi.photoInfo)
                            {
                                item.status = (int)PhotoStatus.NoDistributePrinter;//修改照片状态为“未分配打印机”
                            }
                            db.SaveChanges();
                            if (!oi.Tb_OrderBagReference.IsLoaded)
                            {
                                oi.Tb_OrderBagReference.Load();
                            }
                            Tb_OrderBag tb_order = oi.Tb_OrderBag;
                            if (!tb_order.orderInfo.IsLoaded)
                            {
                                tb_order.orderInfo.Load();
                            }
                            //修改订单状态
                            bool flag = true;
                            foreach (var item in tb_order.orderInfo)
                            {
                                if (item.status == (int)OrderInfoStatus.WaitPhotoMove || item.status == (int)OrderInfoStatus.WaitPayAndWaitMovePhoto)
                                {
                                    flag = false;
                                    break;
                                }
                            }
                            if (flag)//如果该订单下所有产品都移动完成
                                tb_order.Status = (int)OrderBagStatus.Doing;
                            db.SaveChanges();
                        }
                        else
                        {
                            result = false;
                        }

                        //tran.Commit();
                    //}
                }
            }
            catch(Exception ex)
            {
                result = false;
                WriteLogForCheck.WriteLogFile(DateTime.Now+":"+ex.Message);

            }
            return result;
        }
    }
}
